package apigateway.requestAtomService;

import apigateway.serviceRegister.Anwser;
import apigateway.serviceRegister.Dependecy;
import apigateway.serviceRegister.User;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * Created by 小泽玛丽罗 on 2017/3/5.
 */
public class AtomServiceRunnable implements Runnable {
    private Logger logger= LoggerFactory.getLogger(AtomServiceRunnable.class);
    private List<Anwser> anwsers;
    private int index;
    private String apiGatewayIp;
    private Dependecy dependecy;
    private CountDownLatch countDownLatch;
    private String uuid;
    public AtomServiceRunnable(List<Anwser> anwsers, int index
            ,String apiGatewayIp,Dependecy dependecy
            ,CountDownLatch countDownLatch,String uuid) {
        this.anwsers = anwsers;
        this.index = index;
        this.apiGatewayIp=apiGatewayIp;
        this.dependecy=dependecy;
        this.countDownLatch=countDownLatch;
        this.uuid=uuid;
    }

    @Override
    public void run() {
        CloseableHttpClient httpClient= HttpClients.createDefault();
        CloseableHttpResponse httpResponse = null;
        try {
            URI uri=new URIBuilder().setScheme("http")
                    .setHost(apiGatewayIp)
                    .setPath(dependecy.getServiceName())
                    .setParameter("location",dependecy.getLocation())
                    .setParameter("type","atom")
                    .setParameter("serviceCategory",dependecy.getServiceCategory())
                    .setParameter("uuid",uuid)
                    .build();
            HttpGet httpGet=new HttpGet( uri);
            httpGet.setHeader("Connection","close");
            httpResponse=httpClient.execute(httpGet);
            String result= EntityUtils.toString(httpResponse.getEntity());
            httpResponse.close();
            JSONObject jsonObject= JSON.parseObject(result);
            JSONArray jsonArray=jsonObject.getJSONArray("anwsers");
            List<User> users= jsonArray.toJavaList(User.class);
            Anwser<User> anwser=new Anwser();
            anwser.getAnwsers().addAll(users);
            anwsers.add(anwser);
            countDownLatch.countDown();
        } catch (IOException e) {
            countDownLatch.countDown();
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        } catch (URISyntaxException e) {
            countDownLatch.countDown();
            logger.error(e.getMessage(),e);
            e.printStackTrace();
        }catch (Exception e){
            countDownLatch.countDown();
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        }
        finally {
            try {
                httpResponse.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

}
